// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.warehouse;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.model.database.DBColumn;
import org.dengues.model.warehouse.BlockType;
import org.dengues.model.warehouse.ProcessType;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.EList;
import org.eclipse.swt.graphics.Image;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-29 qiang.zhang $
 * 
 */
public class WarehouseLabelUtil {

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getNodeIcon".
     * 
     * @param obj
     * @return
     */
    public static Image getNodeIcon(Object obj) {
        Image image = null;
        if (obj instanceof IWarehouseNode) {
            IWarehouseNode warehouseNode = ((IWarehouseNode) obj);
            if (warehouseNode.getNodeType().equals(ENavNodeType.SIMPLE_FOLDER)) {
                return ImageUtil.getImage(IDenguesSharedImage.WAREHOUSE_FOLDER_CLOSE);
            }
            ENodeCategoryName categoryName = warehouseNode.getCategoryName();
            switch (categoryName) {
            case BIN:
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_TRASH);
            case DATASOURCE:
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_DATASOURCE);
            case TABLE:
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_TABLE);
            case PROCESS:
                image = ImageUtil.getImage(IDenguesSharedImage.VIEW_PROCESS);
                break;
            case QUERY:
                image = ImageUtil.getImage(IDenguesSharedImage.VIEW_QUERY);
                break;
            case DATABASE:
                image = ImageUtil.getImage(IDenguesSharedImage.VIEW_DATA);
                break;
            case REPORTS:
                image = ImageUtil.getImage(IDenguesSharedImage.REPORTS_REPORTS);
                break;
            case SCRIPTS:
                image = ImageUtil.getImage(IDenguesSharedImage.PALETTE_CODE);
                break;
            case CDC_TSYS:
                image = ImageUtil.getImage(IDenguesSharedImage.WAREHOUSE_CDC_SYSTABLE);
                break;
            case XMLFILE:
                image = ImageUtil.getImage(IDenguesSharedImage.XML_CREATE_ACTION);
                break;
            case CSVFILE:
                image = ImageUtil.getImage(IDenguesSharedImage.CSV_CREATE_ACTION);
                break;
            case EXCELFILE:
                image = ImageUtil.getImage(IDenguesSharedImage.EXCEL_CREATE_ACTION);
                break;
            case BLOCKS:
                image = ImageUtil.getImage(IDenguesSharedImage.PALETTE_BLOCK);
                break;
            case COLUMN:
                Object object = warehouseNode.getObject().getData();
                boolean isKey = false;
                if (object instanceof DBColumn) {
                    isKey = ((DBColumn) object).isKey();
                }
                if (isKey) {
                    image = ImageUtil.getImage(IDenguesSharedImage.TABLEVIEW_KEY);
                } else {
                    image = ImageUtil.getImage(IDenguesSharedImage.VIEW_COLUMN);
                }
                break;
            default:
                return ImageUtil.getImage(IDenguesSharedImage.QUERSTION);
            }
        }
        return image;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getBlockRefs".
     * 
     * @return
     */
    public static Map<String, Set<String>> getBlockRefs() {
        List<Object> allProcess = WarehouseResourceFactory.getAllObjectsFromEMF(ENodeCategoryName.PROCESS);
        Map<String, Set<String>> usedIds = new HashMap<String, Set<String>>();
        for (Object object : allProcess) {
            ProcessType type = (ProcessType) object;
            EList<BlockType> block = type.getBlock();
            for (BlockType blockType : block) {
                Set<String> list = usedIds.get(blockType.getUuid());
                if (list == null) {
                    list = new HashSet<String>();
                    usedIds.put(blockType.getUuid(), list);
                }
                list.add(type.getName());
            }
        }
        return usedIds;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getBlockType".
     * 
     * @param uuid
     * @return
     */
    public static ProcessType getBlockType(String uuid) {
        List<Object> allProcess = WarehouseResourceFactory.getAllObjectsFromEMF(ENodeCategoryName.BLOCKS);
        for (Object object : allProcess) {
            if (object instanceof ProcessType) {
                if (uuid.equals(((ProcessType) object).getUuid())) {
                    return (ProcessType) object;
                }
            }
        }
        return null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getWarehouseNode".
     * 
     * @param warehousePath
     * @param diagramName
     * @param tableName
     * @return
     */
    public static String getWarehouseNode(IPath warehousePath, String diagramName, String tableName) {
        if (tableName != null) {
            return IPath.SEPARATOR + warehousePath.toPortableString() + "->" + diagramName + ":" //$NON-NLS-1$ //$NON-NLS-2$
                    + tableName;
        } else {
            return IPath.SEPARATOR + warehousePath.toPortableString() + "->" + diagramName;
        }
    }
}
